clear

* generate raw inputs as in actual randomization 2018
set obs 43
gen judgetype = "Bankruptcy" if _n<=19
replace judgetype = "Circuit" if inrange(_n,20,25)
replace judgetype = "District" if inrange(_n,26,35)
replace judgetype = "Magistrate" if inrange(_n,36,43)


*** randomization: copied from randomization_script_2018_better_direct_to_casecode.do used in 2018 (but taking out "evengroup" lines that weren't used because all set to 0)
* random permutation of (a) judge type# and (b) judge within judge type
gen random = runiform()
bys judgetype: egen judgetypeorder = median(random)
gsort +judgetypeorder +random
drop judgetypeorder random
gen merge_link = mod(_n,8)
tempfile judges
save `judges', replace
* random assignment grid: this creates a 8x3 matrix that has all combinations of 0s and 1s, balanced in each column for every two lines
clear
set obs 8
gen Forum = "W" if mod(_n,2)==1
replace Forum = "S" if mi(Forum)
gen Accident_state = "K" if inlist(_n,1,4,5,8)
replace Accident_state = "N" if mi(Accident_state)
gen Sympathy = "S" if (mod(_n,2)==1 & _n<5) | (mod(_n,2)==0 & _n>=5)
replace Sympathy = "N" if mi(Sympathy)
* randomly permutate blocks of two lines, and within those two lines, to avoid any ordering effect
gen twolines = round(_n/2)
gen random = runiform()
bys twolines: egen twolines_position = max(random)
sort twolines_position random
drop twolines* random
* merge together
gen merge_link = mod(_n,8)
merge 1:m merge_link using `judges', assert(3) nogenerate
drop merge_link

*** re-randomizing to avoid any ordering effect from prior neighboring judgegroups
gen random = runiform()
sort judgetype random
drop random
